// serviceShopRepair.js
const app = getApp()
const util = require("../../utils/util.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    status: 1, //1:待处理，2：已完成，
    list: [],
    detail: [],
    detailShow: false,
  },
  // 触发详情弹窗
  orderDetail: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var detail = that.data.list[index];
    that.setData({
      detailShow: true,
      detail: detail
    })
  },
  onClose() {
    this.setData({ detailShow: false });
  },
  orderlist: function () {
    var that = this;
    var data = {
      id:that.data.id,
      status: that.data.status
    };
    app.post('manage/faultRecord', data).then((res) => {
      if (res.code == 200) {
        var dataArr = res.list;
        console.log(dataArr)
        for (var i = 0; i < dataArr.length; i++) {
          var file = {};
          var fileList = [];
          var imgList = dataArr[i].images.split(",");
          for (var item in imgList) {
            file = {
              'url': imgList[item],
              'isImage': true
            }
            fileList.push(file)
          };
          dataArr[i].images = fileList;
        }
        that.setData({
          list: dataArr
        })
      }
    }).catch((errMsg) => {
      console.log(errMsg);
      wx.showToast({
        title: "网络错误",
        icon: 'loading',
        duration: 2000
      })
    })
    wx.stopPullDownRefresh()
  },
  updateStatus: function (id, status) {
    var that = this;
    var data = {
      id: id, status: status
    };
    wx.showLoading({
      title: '加载中...',
    })
    app.post('manage/repairDevice', data).then((res) => {
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: "修改成功",
          icon: 'success',
          duration: 2000
        })
        this.setData({
          pagenum: 1,
          list: []
        })
        this.orderlist();
      }
    }).catch((errMsg) => {
      console.log(errMsg);
      wx.hideLoading();
      wx.showToast({
        title: "网络错误",
        icon: 'loading',
        duration: 2000
      })
    })
  },
  finishTap: function (e) {
    var id = e.currentTarget.dataset.id;
    this.updateStatus(id, 2)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      status: options.status,
      id:options.id
    })
    this.orderlist();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // 请求最新数据
    this.orderlist();
  }
})